package mhys.View.Teacher;
import mhys.Dao.ManageDao;
import mhys.Dao.ScoreDao;
import mhys.Dao.ClassesDao;
import mhys.Entity.Manage;
import mhys.Entity.Score;
import mhys.Entity.Student;

import javax.swing.*;
import javax.swing.table.DefaultTableModel;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import java.util.List;
public class ScoreJpanel extends JPanel{
    DefaultTableModel model;
    JTable jTable;
    ManageDao dao = new ManageDao();
    ScoreDao scoreDao = new ScoreDao();
    ClassesDao classesDao = new ClassesDao();
    public ScoreJpanel() {
        this.setBackground(Color.WHITE);
        this.setLayout(new BorderLayout(10,10));
        JLabel jLabel = new JLabel("学生姓名:");
        jLabel.setBounds(10,10,120,30);

        JPanel jPanel1 = new JPanel();
        jPanel1.setLayout(null);
        jPanel1.setPreferredSize(new Dimension(100, 60));

        JTextField jTextField = new JTextField();
        jTextField.setBounds(130,12,100,30);

        JButton updateButton = new JButton("修改");
        updateButton.setBounds(240,12,60,30);

        JButton addButton = new JButton("添加");
        addButton.setBounds(310, 12, 60, 30);

        addButton.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                final JDialog jDialog = new JDialog(App.jFrame,"添加信息",true);
                jDialog.setSize(300, 360);
                jDialog.setLocationRelativeTo(App.jFrame);
                jDialog.setLayout(null);
                JLabel idJLabel = new JLabel("学生ID：");
                idJLabel.setBounds(10, 20, 80, 25);
                final JTextField idTextField = new JTextField();
                idTextField.setBounds(90, 20, 150, 25);

                JLabel objJlabel = new JLabel("课程：");
                objJlabel.setBounds(10, 60, 80, 25);
                final JTextField  objTextField = new JTextField();
                objTextField.setBounds(90, 60, 150, 25);

                JLabel scoreJLabel = new JLabel("分数：");
                scoreJLabel.setBounds(10, 100, 80, 25);
                final JTextField scoreTextField = new JTextField();
                scoreTextField.setBounds(90, 100, 150, 25);

                JLabel pointJLabel = new JLabel("绩点：");
                pointJLabel.setBounds(10, 140, 80, 25);
                final JTextField pointTextField = new JTextField();
                pointTextField.setBounds(90, 140, 150, 25);

                JButton okButton = new JButton("添加");
                okButton.setBounds(100, 180, 80, 25);

                okButton.addActionListener(new ActionListener() {
                    @Override
                    public void actionPerformed(ActionEvent e) {
                        Integer id =Integer.parseInt(idTextField.getText());
                        String obj = objTextField.getText();
                        Double score = Double.parseDouble(scoreTextField.getText());
                        Double point = Double.parseDouble(pointTextField.getText());
                        Score score1 = new Score(id,obj,score,point);
                        int addScore = scoreDao.addScore(score1);
                        System.out.println(addScore);
                        if (addScore == 1){
                            List<Manage> manageList = dao.getStuScore();
                            JOptionPane.showMessageDialog(null, "添加成功");
                            jDialog.dispose();
                            initData(manageList);
                            jTable.clearSelection();
                            idTextField.setText("");
                            objTextField.setText("");
                            scoreTextField.setText("");
                            pointTextField.setText("");
                        }else {
                            JOptionPane.showMessageDialog(null, "添加失败");
                        }
                    }
                });


                jDialog.add(idJLabel);
                jDialog.add(idTextField);
                jDialog.add(objJlabel);
                jDialog.add(objTextField);
                jDialog.add(scoreJLabel);
                jDialog.add(scoreTextField);
                jDialog.add(pointJLabel);
                jDialog.add(pointTextField);
                jDialog.add(okButton);
                jDialog.setVisible(true);
            }
        });

        jTextField.addKeyListener(new KeyAdapter() {
            @Override
            public void keyReleased(KeyEvent e) {
                String key = jTextField.getText();
                List<Manage> manageList = dao.getStuByName(key);
                if (manageList != null){
                    initData(manageList);
                }else{
                    JOptionPane.showMessageDialog(null,"查无此人");
                }
            }
        });

        updateButton.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                String[] rowData = new String[jTable.getColumnCount()];
                for (int i = 0; i < jTable.getColumnCount(); i++) {
                    rowData[i] = jTable.getValueAt(jTable.getSelectedRow(), i).toString();
                    System.out.println( rowData[i]);
                }

                final JDialog jDialog = new JDialog(App.jFrame,"修改信息",true);
                jDialog.setSize(300, 360);
                jDialog.setLocationRelativeTo(App.jFrame);
                jDialog.setLayout(null);
                JLabel idJLabel = new JLabel("学生ID：");
                idJLabel.setBounds(10, 20, 80, 25);
                final JTextField idTextField = new JTextField( rowData[0]);
                idTextField.setBounds(90, 20, 150, 25);

                JLabel objJlabel = new JLabel("课程：");
                objJlabel.setBounds(10, 60, 80, 25);
                final JTextField  objTextField = new JTextField(rowData[3]);
                objTextField.setBounds(90, 60, 150, 25);

                JLabel scoreJLabel = new JLabel("分数：");
                scoreJLabel.setBounds(10, 100, 80, 25);
                final JTextField scoreTextField = new JTextField(rowData[4]);
                scoreTextField.setBounds(90, 100, 150, 25);

                JLabel pointJLabel = new JLabel("绩点：");
                pointJLabel.setBounds(10, 140, 80, 25);
                final JTextField pointTextField = new JTextField(rowData[5]);
                pointTextField.setBounds(90, 140, 150, 25);

                JButton okButton = new JButton("修改");
                okButton.setBounds(100, 180, 80, 25);

                okButton.addActionListener(new ActionListener() {
                    @Override
                    public void actionPerformed(ActionEvent e) {
                        int index = jTable.getSelectedRow();
                        if (index != -1) {
                            Integer s_id = (Integer)jTable.getValueAt(index,0);
                            String obj = objTextField.getText();
                            Double score = Double.parseDouble(scoreTextField.getText());
                            Double point = Double.parseDouble(pointTextField.getText());
                            Score score1 = new Score(s_id,obj,score,point);
                            int updateScore = scoreDao.updateScore(score1);
                            System.out.println(updateScore);
                            if (updateScore == 3){
                                List<Manage> manages = dao.getStuScore();
                                JOptionPane.showMessageDialog(null, "修改成功");
                                jDialog.dispose();
                                initData(manages);
                            }
                        }else {
                            JOptionPane.showMessageDialog(null, "请选择需要修改的用户");
                        }
                    }
                });


                jDialog.add(idJLabel);
                jDialog.add(idTextField);
                jDialog.add(pointTextField);
                jDialog.add(pointJLabel);
                jDialog.add(okButton);
                jDialog.add(scoreJLabel);
                jDialog.add(scoreTextField);
                jDialog.add(objTextField);
                jDialog.add(objJlabel);
                jDialog.setVisible(true);
            }
        });
        jPanel1.add(addButton);
        jPanel1.add(updateButton);
        jPanel1.add(jTextField);
        jPanel1.add(jLabel);


        jLabel.setFont(new Font("微软雅黑", Font.PLAIN, 24));
        String[] header = {"学生ID","学号","学生姓名","课程","分数","绩点"};
        model = new DefaultTableModel(header,0){
            public boolean isCellEditable(int row,int column){
                return false;
            }
        };
        jTable = new JTable(model);
        jTable.getTableHeader().setReorderingAllowed(false);
        jTable.setRowHeight(30);
        JScrollPane jScrollPane = new JScrollPane(jTable);
        List<Manage> manageList = dao.getStuScore();
        initData(manageList);
        jScrollPane.getViewport().setBackground(Color.WHITE);
        this.add(jPanel1,BorderLayout.NORTH);
        this.add(jScrollPane,BorderLayout.CENTER);

    }
    public  void initData(List<Manage> managesList) {
        model.getDataVector().clear();
        for (Manage manage : managesList) {
            Object[] data = {manage.getS_id(),manage.getS_number(),manage.getS_name(),manage.getObject(),manage.getScore(),manage.getPoint()};
            model.addRow(data);
        }
        jTable.clearSelection();
    }
}
